package main

import (
	"github.com/urfave/cli"
	"os"
	"xlq-demo/library/apisync"
	"xlq-demo/library/dbsync"
	"os/exec"
	"io/ioutil"
	"net/http"
	"fmt"
	"strings"
)

func main() {
	c := cli.NewApp()
	c.Name = "demo"
	c.Usage = "demo 1.0"
	c.Commands = []cli.Command{
		{
			Name: "dbsync",
			Usage: "同步数据库结构体",
			Action: func(ctx *cli.Context) error {
				dbsync.SyncRun(&dbsync.SyncRunIn{
					Root:        "xlq-demo",
					DbType:      "pgsql",
					DbHost:      "123.207.23.197",
					DbPort:      "5432",
					DbUser:      "postgres",
					DbPsd:       "postgres@123456",
					DbName:      "xqt_dev",
					TablePrefix: "xqt_",
				})

				/*dbsync.SyncRun(&dbsync.SyncRunIn{
					Root: "xlq-demo",
					DbType: "mysql",
					DbHost: "47.241.4.152",
					DbPort: "3308",
					DbUser: "root",
					DbPsd: "root",
					DbName: "test",
					TablePrefix: "",
				})*/
				return nil
			},
		},
		{
			Name: "apisync",
			Usage: "同步api",
			Action: func(ctx *cli.Context) error {
				apisync.Run(&apisync.RunIn{
					Root: "xlq-demo",
					Dir:  "app/api",
					File: "./conf/api.conf",
				})

				return nil
			},
		},
		{
			Name: "yapi",
			Usage: "同步api",
			Action: func(ctx *cli.Context) error {
				err := exec.Command("swag", "init").Run()
				if err != nil {
					return err
				}
				filename := "./docs/swagger.json"
				fs,err := ioutil.ReadFile(filename)
				if err != nil {
					return err
				}
				client := &http.Client{}

				yapiUrl := "http://47.110.129.100:3000/api/open/import_data"
				yapiToken := "15331acec9348f6232a3"
				body := fmt.Sprintf("type=swagger&token=%s&json=%s&merge=merge",yapiToken,string(fs))
				req,err := http.NewRequest("POST",yapiUrl,strings.NewReader(body))
				req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
				if err != nil {
					return err
				}
				res,err := client.Do(req)
				if err != nil {
					return err
				}
				defer res.Body.Close()
				resBys,_ := ioutil.ReadAll(res.Body)
				fmt.Println(string(resBys))
				return nil
			},
		},
		{
			Name:  "test",
			Usage: "测试",
			Action: func(context *cli.Context) error {


				return nil
			},
		},

	}
	c.Run(os.Args)
}


